package com.activemq.spring;

import cn.hutool.core.util.StrUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

@Slf4j
@Component
public class MyMessageListener implements MessageListener {
    private int i = 0;

    @Override
    public void onMessage(Message message) {
        i++;
        String msg = "";
        try {
            if (message instanceof TextMessage) {
                msg = ((TextMessage) message).getText();
                log.info("消息=" + msg);
            }
        } catch (JMSException e) {
            throw new RuntimeException(e);
        }
        if (StrUtil.contains(msg, "重试")) {
            throw new RuntimeException("故意");
        }
    }
}
